package cn.edu.lingnan.tourism.service.Impl;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.mapper.CategoryMapper;
import cn.edu.lingnan.tourism.mapper.RouteMapper;
import cn.edu.lingnan.tourism.mapper.SellerMapper;
import cn.edu.lingnan.tourism.pojo.Category;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.pojo.Seller;
import cn.edu.lingnan.tourism.pojo.User;
import cn.edu.lingnan.tourism.service.RouteService;
import cn.edu.lingnan.tourism.service.SellerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RouteServiceImpl implements RouteService {
    @Resource
    private RouteMapper routeMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private SellerMapper sellerMapper;
    @Override
    public List<Route> getAllRoutes() {
        List<Route> routes= routeMapper.selectAll();
       return routes;
    }
@Transactional(readOnly = true)
    @Override
    public List<Route> searchRoutes(Route route) throws TourismException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        if("".equals(route.getRdate())){
            route.setRdate(null);
        }
        List<Route> routes = routeMapper.getRoutesLiketStr(route, now);
        return routes;
    }
@Transactional(readOnly = true)
    @Override
    public Map getRouteDetails(Integer routeId) throws TourismException {
        Map<String, Object> map=new HashMap<>();
        Route route= routeMapper.selectByPrimaryKey(routeId);
        if(route!=null){
            Category category = categoryMapper.selectByPrimaryKey(route.getCid());
            Seller seller = sellerMapper.selectByPrimaryKey(route.getSid());
            map.put("route",route);
            map.put("seller",seller);
            map.put("category",category);
            return map;
        }else {
            throw new TourismException("查询失败");
        }
    }

@Transactional(readOnly = true)
    @Override
    public Map<String, List<Route>> getRouteMap() {
        Map<String,List<Route>> routeMap=new HashMap<String,List<Route>>();
        List<Category> categories = categoryMapper.selectAll();
        categories.forEach(category -> {
            List<Route> routes = routeMapper.selectRouteByCid(category.getCid());
            if(routes.size()>0){
                routeMap.put(category.getCname(),routes);
            }
        });
        return routeMap;
    }
    @Transactional(readOnly = true)
    @Override
    public List<Route> getHotRoutes() {
        return routeMapper.selectHotRoute();
    }
@Transactional(readOnly = true)
    @Override
    public List<Route> getNewRoutes() {
        return routeMapper.selectNewRoute();
    }


}
